package com.zdkj.subway.check.util;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.core.util.R;
import com.zdkj.common.core.util.SpringContextHolder;
import com.zdkj.subway.check.mapper.SysUserMapper;
import feign.Contract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.SQLException;

/**
 * 密码校验
 */
public class CheckAccount {

    public static R checkAccount(String usercode,String password){
        PasswordEncoder ENCODER = new BCryptPasswordEncoder();
        SysUserMapper bean = SpringContextHolder.getBean(SysUserMapper.class);
        Map<String,Object> pa=new HashMap<>();
        pa.put("user_code",usercode);
        List<SysUser> sysUsers = bean.selectByMap(pa);
        if(sysUsers==null || sysUsers.size()==0){
            return R.failed("用户名或密码错误");
        }
        return R.ok(ENCODER.matches(password,sysUsers.get(0).getPassword()));
    }
   //获取售票员通知期限
	public static int getConductorNoticeLimit(){
	   return  getLimitTime("CONDUCTOR_NOTICE_TIME");
	}
	//获取车站通知期限
	public static int getStationNoticeLimit(){
	   return  getLimitTime("STATION_NOTICE_TIME");
	}
   //获取时间类各种参数值
	public static int  getLimitTime(String key) {
	    try{
		   String sw_pm_time_sql = "select * from sw_pm_time";
           List<Map<String, Object>> sw_pm_time_resp = JdbcUtil.exeSqlQuery(sw_pm_time_sql);
           Object conductorNoticeLimit = sw_pm_time_resp.get(0).get(key);
		   if(conductorNoticeLimit==null){
		      return 3;
		   }else{
		     return Integer.valueOf(conductorNoticeLimit.toString());
		  }
		}catch(SQLException e){
		   return 3;
		}
		
	 }

}
